﻿using System;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace woaC2.Common
{
    public class FunctionCmn
    {
        /// <summary>
        /// Chuyển từ mảng byte sang string ASCII 
        /// </summary>
        /// <param name="buff"></param>
        /// <returns></returns>
        public static string ByteToString(byte[] buff)
        {
            string sbinary = "";

            for (int i = 0; i < buff.Length; i++)
            {
                sbinary += buff[i].ToString("x2"); 
            }
            return (sbinary);
        }

        /// <summary>
        /// Get Websocket Key
        /// </summary>
        /// <param name="secWebSocketKey"></param>
        /// <returns></returns>
        public static string GetAcceptKey(string secWebSocketKey)
        {
            const String MagicKEY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
            String secWebSocketAccept = String.Empty;

            // 1. Combine the request Sec-WebSocket-Key with magic key.
            String ret = secWebSocketKey + MagicKEY;

            // 2. Compute the SHA1 hash
            SHA1 sha = new SHA1CryptoServiceProvider();
            byte[] sha1Hash = sha.ComputeHash(Encoding.UTF8.GetBytes(ret));

            // 3. Base64 encode the hash
            secWebSocketAccept = Convert.ToBase64String(sha1Hash);

            return secWebSocketAccept;
        }


        /// <summary>
        /// Kiểm tra toàn vẹn chuỗi thông tin
        /// </summary>
        /// <param name="inputStr"></param>
        /// <param name="hmac"></param>
        /// <returns></returns>
        public static bool IntegrityChk(string inputStr, out string nameMethod, out string jsonStr)
        {
            nameMethod = string.Empty; 
            jsonStr = string.Empty;

            if (inputStr.Length < 65) return false; // nếu là MD5 <33

            string[] parInput = inputStr.Split('$');

            if (parInput.Count() != 3) return false;

            //Private Key
            string privateKey = "idearixT2K";// "asdxxx";

            ASCIIEncoding encoding = new ASCIIEncoding();
          
            HMACSHA256 hmacsha256 = new HMACSHA256(encoding.GetBytes(privateKey));

            byte[] inputStrBytes = encoding.GetBytes(parInput[0] + "$" + parInput[1]);

            byte[] hashInputStr = hmacsha256.ComputeHash(inputStrBytes);

            string hmacCal = ByteToString(hashInputStr);

            if (parInput[2].ToUpper() != hmacCal.ToUpper())
            {
                return false;
            }

            nameMethod = parInput[0];
            jsonStr = parInput[1];
            
            return true;
        }
        
        /// <summary>
        /// Dán tem bảo đảm cho gói tin
        /// </summary>
        /// <param name="inputStr"></param>
        /// <returns></returns>
        public static string StampMessage(string inputStr)
        {
            //Private Key
            string privateKey = "idearixT2K";

            ASCIIEncoding encoding = new ASCIIEncoding();

            HMACSHA256 hmacsha256 = new HMACSHA256(encoding.GetBytes(privateKey));

            byte[] inputStrBytes = encoding.GetBytes(inputStr);

            byte[] hashInputStr = hmacsha256.ComputeHash(inputStrBytes);

            string hmacCal = ByteToString(hashInputStr);

            return inputStr + "$" + hmacCal;
        }

    }
}
